<template>
  <abstract-dialog
    :loading="loading"
    append-to-body
    :title="title"
    :value="value"
    @close="cancel"
    @open="open"
  >
    <abstract-form ref="form" :model="form" :rules="rules" label-width="130px">
      <el-form-item label="特殊人群名称" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <!-- 阻止回车时页面刷新 -->
      <input type="text" style="display: none" />
    </abstract-form>
    <template v-slot:footer>
      <el-button plain size="small" @click="closeDialog">取 消</el-button>
      <el-button size="small" type="primary" @click="confirm">确 定</el-button>
    </template>
  </abstract-dialog>
</template>

<script>
import AbstractDialog from '@/components/abstract/Dialog'
import dialogMixin from '@/mixin/dialogMixin'
import AbstractForm from '@/components/abstract/Form'
import { elSuccess } from '@/utils/message'
import { request } from '@/api/request'

export default {
  name: 'AddDialog',

  components: {
    AbstractDialog,
    AbstractForm
  },

  mixins: [dialogMixin],

  props: {
    value: Boolean,
    data: { type: Object, default: () => ({}) },
    users: {
      type: Array,
      default: () => []
    }
  },

  data() {
    return {
      users_id: '',
      loading: false,
      title: '添加特殊人群标记',
      form: {
        title: null
      },
      rules: {
        title: [
          { required: true, message: '请输入特殊人群名称', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    open() {
      // this.loading = true
      // request('company/users/specialCrowdSign/add', {})
    },

    clearForm() {
      this.form.title = null
      this.$nextTick(() => this.$refs.form.clearValidate())
    },

    cancel() {
      this.closeDialog()
      this.clearForm()
    },

    confirm() {
      if (this.loading) return
      this.$refs.form.validate((v) => {
        if (!v) return
        this.loading = true
        const promise = request(
          'company/users/specialCrowdSign/addSubmit',
          this.form,
          () => {}
        )
        promise
          .then(({ data }) => {
            elSuccess()
            this.$emit('success', data)
            this.cancel()
          })
          .finally(() => this.$nextTick(() => (this.loading = false)))
      })
    }
  }
}
</script>
